package com.xiangban.xiangbanmall.service;

import com.xiangban.xiangbanmall.dao.RelationMapper;
import com.xiangban.xiangbanmall.exception.BizException;
import com.xiangban.xiangbanmall.model.TUserRelation;
import com.xiangban.xiangbanmall.model.dto.UserRelation;
import com.xiangban.xiangbanmall.model.res.ApiResultCode;
import com.xiangban.xiangbanmall.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.xiangban.xiangbanmall.utils.CommonUtils.BizEmpty;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/11/2 16:19
 */
@Service
public class UserRelationService {

    @Autowired
    private RelationMapper userRelationMapper;
    @Autowired
    private CommonUtils commonUtils;


    public Object getRelation() {
       return userRelationMapper.selectRelationList();
    }


    public Object getUserRelation() {
//        if (BizEmpty(commonUtils.getUserIdByToken())){
//            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
//        }
        int userId = Integer.parseInt(commonUtils.getUserIdByToken());
        //TODO 绑定列表
        List<UserRelation> tUserRelations = userRelationMapper.selectUserRelationList(userId);
        if (tUserRelations.size()>0){
            for (UserRelation tUserRelation : tUserRelations) {
                int idCard = Integer.parseInt(tUserRelation.getIdCard().substring(6, 10));
                int time = Integer.parseInt(new SimpleDateFormat("yyyy").format(new Date()));
                int age = time - idCard;//当前用户年龄
                tUserRelation.setAge(age);
            }
        }
        return tUserRelations;
    }

    public void addUserRelation(TUserRelation tUserRelation) throws Exception {
        Boolean aBoolean = checkAddUserRelation(tUserRelation);
        if (!aBoolean){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        tUserRelation.setUserId(Integer.parseInt(commonUtils.getUserIdByToken()));
        List<UserRelation> tUserRelations = userRelationMapper.selectUserRelationList(tUserRelation.getUserId());
        if (tUserRelations.size() >4){
            throw new BizException(ApiResultCode.MAXIMUM_NUMBER_OF_PEOPLE);
        }
        //TODO 验证名称和身份证号
        Boolean aBoolean1 = commonUtils.checkIdCard(tUserRelation.getIdCard(), tUserRelation.getName());
        if (!aBoolean1){
            throw new BizException(ApiResultCode.ID_CARD_AND_NAME_INCONSISTENT);
        }
        userRelationMapper.insertUserRelation(tUserRelation);
    }


    private Boolean checkAddUserRelation(TUserRelation tUserRelation){
//        if (BizEmpty(commonUtils.getUserIdByToken())){
//            return false;
//        }
        if (BizEmpty(tUserRelation.getName())){
            return false;
        }
        if (BizEmpty(tUserRelation.getSex())){
            return false;
        }
        if (BizEmpty(tUserRelation.getPhone())){
            return false;
        }
        if (BizEmpty(tUserRelation.getIdCard())){
            return false;
        }
        if (BizEmpty(tUserRelation.getRelation())){
            return false;
        }
        return true;
    }

    public void deleteUserRelation(TUserRelation tUserRelation) {
//        if (BizEmpty(commonUtils.getUserIdByToken())){
//            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
//        }
        if (BizEmpty(tUserRelation.getId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        tUserRelation.setUserId(Integer.parseInt(commonUtils.getUserIdByToken()));
        userRelationMapper.updateUserRelationType(tUserRelation);
    }

}
